package com.songlanyun.modules.information.controller.admin;

import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.R;
import com.songlanyun.common.validator.ValidatorUtils;
import com.songlanyun.common.validator.group.AddGroup;
import com.songlanyun.common.validator.group.UpdateGroup;
import com.songlanyun.modules.information.entity.InformationEntity;
import com.songlanyun.modules.information.model.dto.InformationDto;
import com.songlanyun.modules.information.model.vo.InformationVo;
import com.songlanyun.modules.information.service.InformationService;
import com.songlanyun.modules.user.controller.AbstractController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


/**
 * 资讯
 *
 * @author WuChao
 * @email songlanyun@gmail.com
 * @date 2022-11-18 09:52:58
 */
@RestController
@RequestMapping("admin/information")
public class InformationAdminController extends AbstractController {
    @Autowired
    private InformationService informationService;

    /**
     * 列表
     */
    @GetMapping("/page")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = informationService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        InformationVo vo = informationService.info(id);
        return R.ok().put("data", vo);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody InformationDto dto) {
        ValidatorUtils.validateEntity(dto, AddGroup.class);
        informationService.add(getUserId(), dto);
        return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public R update(@RequestBody InformationDto dto) {
        ValidatorUtils.validateEntity(dto, UpdateGroup.class);
        informationService.edit(getUserId(), dto);
        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable("id") Long id) {
        informationService.delete(id);
        return R.ok();
    }

    /**
     * 置顶
     */
    @PostMapping("/top")
    public R top(@RequestBody InformationDto dto) {
        informationService.top(dto);
        return R.ok();
    }

    /**
     * 推荐
     */
    @PostMapping("/recommend/{id}")
    public R recommend(@PathVariable("id") Long id) {
        informationService.recommend(id);
        return R.ok();
    }
}
